using System;
using System.Web;
using System.Web.Mvc;
using Manazra.Models.Repository;
using Manazra.Models;
using Manazra.Helpers;
using Manazra.Controllers.Filters;

namespace Manazra.Controllers
{
    [BasePreControllerActionFilter]
    public class CommentsController : Controller
    {
        //comments repository
        private CommentRepository commentRep = new CommentRepository();

        //adds a comment
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Add(int id, string type, string content)
        {
            ControllerStatus result = new ControllerStatus(0, "");
            //get authentication object from the view data
            UserAuth UserAuth = (UserAuth)ViewData["UserAuth"];
            if (UserAuth.Authenticated)
            {
                //get parent segment
                ArticleSegment segment = commentRep.GetParent(id);
                if (segment != null)
                {
                    //create new response row
                    Comment comment = new Comment();

                    //set its properties                    
                    comment.TypeId = commentRep.GetTypeByName(type).RecId;
                    comment.PostedBy = UserAuth.UserName;
                    comment.PostedDate = DateTime.Now;
                    comment.Content = content;

                    //add
                    segment.Comments.Add(comment);
                    //add points for comment to article
                    commentRep.AddPoints(comment);

                    //save changes
                    commentRep.Save();

                    //for facebook users we publish the news for the comment to facebook
                    if (UserAuth.IsFacebookUser)
                    {
                        new FBPublisher().PublishNewCommentFeed(UserAuth, comment);
                    }

                    //set return status
                    result.Status = 1;
                    result.Message = comment.RecId + "-" + UserAuth.FormattedUserName + " wrote at " + String.Format("{0:g}", comment.PostedDate);
                }
                else
                    result.Message = "Parent (" + id + ") not found";
            }
            else
                result.Message = "User not logged in";

            return Json(result);
        }

        //deletes a comment
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Delete(int id)
        {
            ControllerStatus result = new ControllerStatus(0, "");
            //get comment
            var comment = commentRep.GetById(id);
            if (comment != null)
            {
                //get authentication object from the view data
                UserAuth UserAuth = (UserAuth)ViewData["UserAuth"];
                //access is restricted to authors
                if (UserAuth.Authenticated && comment.PostedBy.Equals(UserAuth.UserName, StringComparison.InvariantCulture))
                {
                    //delete response
                    commentRep.Delete(comment);
                    //delete points from article for this comment
                    commentRep.DelPoints(comment);
                    //commit changes
                    commentRep.Save();
                    //set return status
                    result.Status = 1;
                    result.Message = "success";
                }
                else
                    result.Message = "Access Denied";
            }
            else
                result.Message = "Comment (" + id + ") not found";

            return Json(result);
        }

        //returns response types json
        public JsonResult Types()
        {
            //get all reponse types
            return this.Json(commentRep.GetTypes());
        }
    }
}
